package questionmanager;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;

import gui.CustomJScrollPane;
import gui.CustomJTable;
import gui.GUIMain;
import gui.SearchTextField;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;

import rmi.Question;

public class GUIQuestionDisplay extends JPanel{

	private static final long serialVersionUID = 1L;
	public static final int FROM_QUESTIONTYPE = 1;
	public static final int FROM_EXAM= 2;
	
	private SearchTextField searchTF;
	private JTable display;
	private QuestionModel displayModel;
	private int[] readingParam;
	private int getQuestionFrom;
	private GUIQuestionCellEditor ex;
	
	public GUIQuestionDisplay(int getQuestionFrom, boolean isShowPriority, boolean isEditable) {
		this.getQuestionFrom = getQuestionFrom;
		setOpaque(false);
		setLayout(new BorderLayout());
		
		searchTF = new SearchTextField();
		searchTF.getDocument().addDocumentListener(new SearchListener());
		add(searchTF, BorderLayout.NORTH);
		
		displayModel = new QuestionModel(isEditable);
		display = new CustomJTable(displayModel);
		ex = new GUIQuestionCellEditor(isShowPriority);
		
		display.setDefaultEditor(Object.class, ex);
		display.setDefaultRenderer(Object.class, ex);
		display.putClientProperty("terminateEditOnFocusLost", Boolean.FALSE);
		display.setTableHeader(null);
		
		JScrollPane pane = new CustomJScrollPane(display);
		pane.setOpaque(false);
		pane.setBorder(new EmptyBorder(2, 0, 2, 0));
		add(pane, BorderLayout.CENTER);
	}
	
	public QuestionModel getModel(){
		return displayModel;
	}
	
	public JTable getJTable(){
		return display;
	}
	
	public int[] getReadingParam(){
		return readingParam;
	}
	
	public GUIQuestionCellEditor getCellEditor(){
		return this.ex;
	}
	
	public void getQuestionFromDB(int[] readingParam){
		if(display.isEditing()){
			display.getCellEditor().stopCellEditing();
		}
		if(readingParam == null){
			displayModel.clearQuestions();
			displayModel.search(searchTF.getText());
			return;
		}
		
		try {
			displayModel.clearQuestions();
			ArrayList<Question> array = new ArrayList<>();
			this.readingParam = readingParam;
			for(int i = 0 ; i < this.readingParam.length; i++){
				if(getQuestionFrom == FROM_QUESTIONTYPE){
					array.addAll(GUIMain.getDb().getQuestionsByType(this.readingParam[i]));
				}else{
					array.addAll(GUIMain.getDb().getQuestionsByExam(this.readingParam[i]));
				}
			}
			for(Question e : array){
				displayModel.addQuestion(e);
			}		
			displayModel.search(searchTF.getText());
			repaint();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	class SearchListener implements DocumentListener{

		@Override
		public void changedUpdate(DocumentEvent e) {
			search();
		}

		@Override
		public void insertUpdate(DocumentEvent e) {
			search();
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			search();
		}
		
		public void search(){
			display.clearSelection();
			displayModel.search(searchTF.getText().trim());
		}
	}
	
	public class QuestionModel extends AbstractTableModel {
		private static final long serialVersionUID = 1L;
		private boolean isEditable; 
		private String[] columnNames = {"Question"};
		
		private ArrayList<Question> collection;
		private ArrayList<Question> displayCollection;
		
		public QuestionModel(boolean isEditable) {
			this.isEditable = isEditable;
			collection = new ArrayList<>();
			displayCollection = new ArrayList<>();
		}
		
		public ArrayList<Question> getDisplayCollection(){
			return this.displayCollection;
		}
		
		public void addQuestion(Question param){
			collection.add(param);
			fireTableDataChanged();
		}
		
		public void clearQuestions(){
			displayCollection.clear();
			collection.clear();
			fireTableDataChanged();
		}
		
		@SuppressWarnings("unchecked")
		public void displayAll(){
			displayCollection = (ArrayList<Question>) collection.clone();
			fireTableDataChanged();
		}
		
		public void search(String param){
			displayCollection.clear();
			if(param.equals("")){
				displayAll();
				return;
			}
			for(Question e : collection){
				if(e.toString().toLowerCase().contains(param.toLowerCase())){
					displayCollection.add(e);
				}
			}
		}

		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public int getRowCount() {
			return displayCollection.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return displayCollection.get(rowIndex);
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return displayCollection.getClass();
		}

		@Override
		public String getColumnName(int column) {
			return columnNames[column];
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return isEditable;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			//super.setValueAt(aValue, rowIndex, columnIndex);
		}
		
		public boolean getEditable(){
			return this.isEditable;
		}
		
		public void setEditable(boolean param){
			this.isEditable = true;
		}
	}
}
